package com.share.alibaba.auser.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * Hystrix的超时时间必须要比resttemplate大,否则resttemplate会无效，熔断提前返回
 */
@Configuration
public class RestTemplateConfig {

    @Bean("clientHttpRequestFactory")
    public ClientHttpRequestFactory clientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(3000);//单位为ms socketTimeout与ReadTimeout是一样的,表示等待数据的时间或者两个包之间的间隔时间
        factory.setConnectTimeout(1000);//单位为ms
        return factory;
    }

    @Bean
    public RestTemplate restTemplate(@Qualifier("clientHttpRequestFactory") ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }
}